package amdocs.jet.pm.net;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Properties;

import org.apache.http.util.ByteArrayBuffer;

import amdocs.jet.pm.Constants;
import android.util.Log;

public class GetURLConnectionData 
{	
	public static String proxy = "";
	public static String port = "";
	public static String username = "";
	public static String password = "";
	
	/**
	 * The method open url connection to israel bank in order to retrieve 
	 * all currencies data of last sale date
	 * 
	 * @return xml string of all currencies
	 */
	public static InputStream getAllInputStreamCurrencyData(String iUrl)
	{
        try
        {
        	Log.i("GetURLConnectionData", "getAllInputStreamCurrencyData:iUrl=" + iUrl);
        	//Authenticator.setDefault(new AuthenticatorImpl(username,password));
        	URL url = new URL(iUrl); 
//        	Properties systemProperties = System.getProperties();
//        	systemProperties.setProperty("http.proxyHost",proxy);
//        	systemProperties.setProperty("http.proxyPort",port);
            URLConnection urlConn = url.openConnection();
            return urlConn.getInputStream();
            
        }
		catch (UnknownHostException uhe) 
		{
			System.err.println("Check your connection");
			return null;
		}
        catch (Exception e)
        {
        	return null;   
        }
	}
	
	public static String getAllCurrencyData(InputStream inputStream)
	{
        BufferedInputStream bis = new BufferedInputStream(inputStream);
           
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        try {
			while((current = bis.read()) != -1)
			{
			    	baf.append((byte)current);
			}
		} catch (IOException e) 
		{
			e.printStackTrace();
			return null;
		}
        return new String(baf.toByteArray());
	}
}
